package com.songminju.jsong_httpserver.server.bio;

import org.apache.log4j.Logger;

import com.songminju.jsong_httpserver.http.HttpRequest;
import com.songminju.jsong_httpserver.http.HttpResponse;

/**
@todo
@description
@author songminju
@date 2017年12月5日
*/
public class HtttpHandlerTask implements Runnable{
	private static HttpEncoder encoder = new HttpEncoder();
	private RequestAdapter adapter;
	private RequestConnection rc;
	private Logger logger = Logger.getLogger(this.getClass());
	public HtttpHandlerTask(RequestConnection rc,RequestAdapter adapter) {
		this.rc = rc;
		this.adapter = adapter;
	}

	@Override
	public void run() {
		logger.debug("handler task socket:"+rc);
		logger.debug("start encoder socket:"+rc);
		HttpRequest req = encoder.resoveRequest(rc.getSocket());
		HttpResponse res = encoder.resoveResponse(rc.getSocket());
		logger.debug("finished encoder socket:"+rc+",start process");
		adapter.getHttpHandler().process(req, res);
		res.end();
		logger.debug("finished process.");
		if(! rc.getSocket().isClosed()) {
			adapter.put(rc);
		}
	}

}
